package github.flandre.examplemod.common.block;

import github.flandre.examplemod.common.tileentity.ExampleGuiTileEntity;
import github.flandre.examplemod.core.init.TileEntityInit;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nullable;
/*
 * ExampleGuiBlock 机器方块本身
 * ExampleGuiTileEntity 机器的实体
 * ExampleGuiContainer 实体的界面
 * DisplayCaseScreen 客户端渲染贴图
 * TileEntityInit 初始化
 * ContainerInit 初始化
 * */
public class ExampleGuiBlock extends Block {
    public ExampleGuiBlock() {
        super(Properties.create(Material.ROCK)
                .hardnessAndResistance(1.5F,100.0F)
                        .sound(SoundType.GLASS) //挖掘声音
                        .harvestLevel(2)  //挖掘等级
                        .harvestTool(ToolType.AXE)); //挖掘工具
    }

    //代表方块存在实体
    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    //代表方块关联的是什么实体
    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return TileEntityInit.ENTITY_TYPE_REGISTRY_GUI.get().create();
    }

    //当方块被点击时
    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if(!worldIn.isRemote)
        {
            //找到这个实体
            TileEntity tileEntity = worldIn.getTileEntity(pos);
            //判断是否为我们写的实体
            if(tileEntity instanceof ExampleGuiTileEntity)
            {
                NetworkHooks.openGui((ServerPlayerEntity) player,
                        (ExampleGuiTileEntity)tileEntity,
                        pos);
            }
        }

        return super.onBlockActivated(state, worldIn, pos, player, handIn, hit);
    }
}
